﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using Lending.RMF.Tool;
using Lending.RMF.AutoProcessService.Interface;

namespace AutoRequest
{
    public class Excutor : IAutoProcess
    {
        public Excutor()
        {
            IAutoProcess.Interval=12;
        }
        public override void Execute(DateTime handleTime, object param)
        {
            if (handleTime.Hour == 8)
            {
                try
                {
                    DateTime lastDoTime = this.GetLastDoTime();
                    LogHelper.Write(CommonLogger.Application, LogLevel.Info, "Start Date:" + lastDoTime.ToString());
                    string constr = "Data Source=Sql1001.webweb.com;Initial Catalog=DB_98EBF3_yaojianzhi;User ID=DB_98EBF3_yaojianzhi_admin;Password=yaojianzhi@2014";
                    IResource resource = new ganjiResource(constr, lastDoTime);
                    LogHelper.Write(CommonLogger.Application, LogLevel.Info, "resource.Do (Begin)");
                    resource.Do();
                    LogHelper.Write(CommonLogger.Application, LogLevel.Info, "resource.Do (End)");
                    this.OverWriteDoTime();
                    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://www.yaojianzhi.com/home/DoIndex");
                    httpWebRequest.UserAgent = "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
                    httpWebRequest.Accept = "*/*";
                    httpWebRequest.KeepAlive = true;
                    httpWebRequest.Headers.Add("Accept-Language", "zh-cn,en-us;q=0.5");
                    HttpWebResponse arg_B2_0 = (HttpWebResponse)httpWebRequest.GetResponse();
                    string text = string.Format("共分析：{0}条数据{1}{2}条数据被记录{3}{4}因为数据异常被跳过{5}{6}因为非记录时间被跳过{7}{8}因为联系方式为空被跳过{9}{10}因为包含敏感文字被跳过{11}{12}因为重复被跳过", new object[]
					{
						resource.total, 
						Environment.NewLine, 
						resource.recrod, 
						Environment.NewLine, 
						resource.jumpwitherror, 
						Environment.NewLine, 
						resource.jumpwithtime, 
						Environment.NewLine, 
						resource.jumpwithempty, 
						Environment.NewLine, 
						resource.jumpwithganji, 
						Environment.NewLine, 
						resource.repeat
					});
                    LogHelper.Write(CommonLogger.Application, LogLevel.Info, text);
                }
                catch (Exception ex)
                {
                    LogHelper.Write(0, 0, ex, "");
                }
            }
        }
        private DateTime GetLastDoTime()
        {
            if (!File.Exists(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "perpare.data"))
            {
                return DateTime.Now;
            }
            DateTime result;
            using (StreamReader streamReader = new StreamReader(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "perpare.data"))
            {
                try
                {
                    string s = streamReader.ReadLine();
                    DateTime now = DateTime.Now;
                    if (DateTime.TryParse(s, out now))
                    {
                        result = now;
                        return result;
                    }
                }
                catch
                {
                }
                result = DateTime.Now;
            }
            return result;
        }
        private DateTime OverWriteDoTime()
        {
            DateTime now = DateTime.Now;
            using (StreamWriter streamWriter = new StreamWriter(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "perpare.data", false))
            {
                streamWriter.Write(now.ToString());
                streamWriter.Flush();
            }
            return now;
        }
    }
}
